package com.octoplay.octoshape;

import java.util.Arrays;
import java.util.LinkedList;

import octoshape.osa2.StreamPlayer;
import android.net.Uri;
import android.util.Log;

public class Stream{
	
	private static final String LOGTAG = "Stream";
	StreamPlayer sp;
	protected boolean osaSeek;
	protected String octoLink;
	protected String selectedPlayerId;
	protected boolean nativeSeek;
	protected long liveMaxLatency;
	protected String xmlMetaData;
	protected byte[] byteMetaData;
	protected Uri playerURL;
	protected long fileDuration;
	protected long osaSeekOffset;
	protected boolean isLive;
	
	// QUEUE FOR MULTIPLE URLS
	public LinkedList<Uri> urlQueue = new LinkedList<Uri>();
	
	
	// STATES
	protected boolean isPlaying = false;
	private boolean isSeeking = false; 
	
	public Stream(StreamPlayer sp, String octoLink) {
		this.sp = sp;
		this.octoLink = octoLink;
	}
	@Override
	public String toString() {
		return "Stream [sp=" + sp + ", osaSeek=" + osaSeek + ", octoLink="
				+ octoLink + ", selectedPlayerId=" + selectedPlayerId
				+ ", nativeSeek=" + nativeSeek + ", streamType="
				+ ", liveMaxLatency=" + liveMaxLatency + ", xmlMetaData="
				+ xmlMetaData + ", byteMetaData="
				+ Arrays.toString(byteMetaData) + ", playerURL="
				+ playerURL + ", fileDuration=" + fileDuration
				+ ", osaSeekOffset=" + osaSeekOffset + "]";
	}
	
	//*************** GETTER METHODS *****************//
	public String getSelectedPlayerId() {
		return selectedPlayerId;
	}
	public long getLiveMaxLatency() {
		return liveMaxLatency;
	}
	public Uri getPlayerURL() {
		return playerURL;
	}
	public long getFileDuration() {
		return fileDuration;
	}
	public long getOsaSeekOffset() {
		return osaSeekOffset;
	}
	public boolean supportsNativeSeek() {
		return nativeSeek;
	}
	public boolean supportsOSASeek() {
		return osaSeek;
	}
	public boolean isLive() {
		return isLive;
	}
	public boolean isOndemand() {
		return !isLive;
	}
	
	
	public void requestPlayback() {
		this.isPlaying = true;
		sp.requestPlay();
	}
	
	// playback state
	public boolean isPlaying(){
		 return isPlaying;
	}
	
	public void hasCompleted() {
		this.playerURL = null;
		this.osaSeekOffset = 0;
		this.isPlaying = false;
		this.isSeeking = false;	
	}
	
	// Seeking state 
	public boolean isSeeking() {
		return this.isSeeking;
	}
	public void setAsSeeking(boolean isSeeking) {
		this.isSeeking = isSeeking;
	}
	public void requestOsaSeek(int offSet) {
		Log.i(LOGTAG, "called seekTo: " + (offSet)  + "msec");
		sp.requestPlayOndemandMediaTimePosition(offSet);
	}
	public void requestDVR(int offset){
		Log.i(LOGTAG, "called playStreamWithLatency: "+offset+"msec");
		sp.requestPlayLiveWithLatency(offset);
	}
}
